package handler

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"logi-flow/cache"
	"logi-flow/model"
	"logi-flow/pck"
	"strconv"
	"time"
)

func Login(c *gin.Context) {
	username := c.PostForm("username")
	password := c.PostForm("password")
	if username == "" || password == "" {
		c.JSON(200, gin.H{"code": 500, "msg": "数据库不能为空"})
		return
	}
	redis_key := "login_errCont_" + username
	errCout, _ := cache.Rdb.Get(cache.Ctx, redis_key).Int()
	if errCout >= 3 {
		c.JSON(200, gin.H{"code": 500, "msg": "密码错误3次，禁止登录"})
		return
	}
	var user model.User
	err := model.DB.Where("username=?", username).Find(&user).Error
	if err != nil {
		c.JSON(200, gin.H{"code": 500, "msg": "查询失败"})
		return
	}
	if user.ID == 0 {
		c.JSON(200, gin.H{"code": 500, "msg": "账号或密码错误"})
		return
	}
	if user.Password != pck.Md5(password) {
		cache.Rdb.Incr(cache.Ctx, redis_key)
		if errCout == 2 {
			cache.Rdb.Set(cache.Ctx, redis_key, 3, time.Hour*2)
			c.JSON(200, gin.H{"code": 500, "msg": "密码错误3次，禁止登录2小时"})
			return
		} else {
			c.JSON(200, gin.H{"code": 500, "msg": "账号或密码错误"})
			return
		}
	}
	c.JSON(200, gin.H{"code": 200, "msg": "登录成功"})
}
func AddUserRode(c *gin.Context) {
	uid, _ := strconv.Atoi(c.PostForm("uid"))
	rid, _ := strconv.Atoi(c.PostForm("rid"))
	userRole := model.UserRole{
		Uid: uid,
		Rid: rid,
	}
	err := model.DB.Create(&userRole).Error
	if err != nil {
		c.JSON(200, gin.H{"code": 500, "msg": "添加失败"})
		return
	}
	c.JSON(200, gin.H{"code": 200, "msg": "添加成功"})
	return
}
func UserRoleList(c *gin.Context) {
	uid := c.Query("uid")
	var role []model.Role
	err := model.DB.Table("roles").Joins("left join user_roles on user_roles.id=roles.id").
		Where("user_roles.uid=?", uid).Find(&role).Error
	if err != nil {
		c.JSON(200, gin.H{"code": 500, "msg": "查询失败"})
		return
	}
	c.JSON(200, gin.H{"code": 200, "msg": "查询成功", "data": role})
	return
}
func RoleNodeList(c *gin.Context) {
	rid := c.Query("rid")
	var node []model.Node
	err := model.DB.Table("nodes").Joins("left join role_nodes on role_nodes.id=nodes.id").
		Where("role_nodes.rid=?", rid).Find(&node).Error
	if err != nil {
		c.JSON(200, gin.H{"code": 500, "msg": "查询失败"})
		return
	}
	c.JSON(200, gin.H{"code": 200, "msg": "查询成功", "data": node})
	return
}
func AddPer(c *gin.Context) {
	per_code := c.PostForm("per_code")
	per_name := c.PostForm("per_name")
	per_phone := c.PostForm("per_phone")
	per := model.PerSon{
		PerCode:  per_code,
		PerName:  per_name,
		PerPhone: per_phone,
	}
	err := model.DB.Create(&per).Error
	if err != nil {
		c.JSON(200, gin.H{"code": 500, "msg": "添加失败"})
		return
	}
	c.JSON(200, gin.H{"code": 200, "msg": "添加成功"})
	return
}
func AddPerOrder(c *gin.Context) {
	order_code := c.PostForm("order_code")
	order_name := c.PostForm("order_name")
	order_form := c.PostForm("order_form")
	order_to := c.PostForm("order_to")
	pid, _ := strconv.Atoi(c.PostForm("pid"))
	perOrder := model.PerSonOrder{
		OrderCode: order_code,
		OrderName: order_name,
		OrderFrom: order_form,
		OrderTo:   order_to,
		Pid:       pid,
	}
	err := model.DB.Create(&perOrder).Error
	if err != nil {
		c.JSON(200, gin.H{"code": 500, "msg": "添加失败"})
		return
	}
	c.JSON(200, gin.H{"code": 200, "msg": "添加成功"})
	return
}
func PerOrderList(c *gin.Context) {
	var perOrder []model.PerSonOrderList
	redis_key := "list"
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		model.DB.Table("per_son_orders").Select("per_code AS PerCode",
			"per_name AS PerName",
			"per_phone AS PerPhone",
			"order_code AS OrderCode",
			"order_name AS OrderName",
			"order_from AS OrderFrom",
			"order_to AS OrderTo").
			Joins("LEFT JOIN per_sons ON per_son_orders.pid = per_sons.id").
			Find(&perOrder)
		jsonData, _ := json.Marshal(perOrder)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &perOrder)
	}
	var total int64
	model.DB.Model(&model.PerSonOrder{}).Count(&total)
	c.JSON(200, gin.H{"code": 200, "msg": "查询成功", "data": perOrder, "total": total})
	return
}
func DetailPerOrderList(c *gin.Context) {
	id := c.Query("id")
	var perOrder model.PerSonOrderList
	redis_key := "list_id" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		model.DB.Table("per_son_orders").Select("per_code AS PerCode",
			"per_name AS PerName",
			"per_phone AS PerPhone",
			"order_code AS OrderCode",
			"order_name AS OrderName",
			"order_from AS OrderFrom",
			"order_to AS OrderTo").
			Joins("LEFT JOIN per_sons ON per_son_orders.pid = per_sons.id").
			Where("per_son_orders.id=?", id).
			Find(&perOrder)
		jsonData, _ := json.Marshal(perOrder)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &perOrder)
	}
	c.JSON(200, gin.H{"code": 200, "msg": "查询成功", "data": perOrder})
	return
}
